package cn.xshi.iot.live.util;

import cn.xshi.common.util.StringUtil;
import cn.xshi.iot.live.config.Config;
import cn.xshi.iot.live.model.CameraEntity;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
 * @Desc 实时流封装
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Component
public class ActualTimeUtil {

    @Resource
    Config config;

    /**
     * 封装实时推流结构
     * @param cameraEntity
     */
    public void doActualTime(CameraEntity cameraEntity){
        String rtsp = "";
        String rtmp = "";
        String ip = IpUtil.IpConvert(cameraEntity.getIp());
        String url = "";
        int port = cameraEntity.getPort()==null?554:cameraEntity.getPort();
        if(StringUtil.isEmpty(cameraEntity.getRtsp())){
            String cameraCode = cameraEntity.getCameraCode();
            switch (cameraCode){
                case Constant.HK://海康
                    String subType = "sub";
                    if( 0== cameraEntity.getSubtype()){
                        subType = "main";
                    }
                    rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/h264/" + cameraEntity.getStream()+"/"+subType + "/av_stream";//海康取码
                    /* rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/h264/" + cameraEntity.getChannel() + "/" + cameraEntity.getStream() + "/av_stream";//海康取码*/
                    break;
                case Constant.DH://大华
                    rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/cam/realmonitor?channel=" + cameraEntity.getChannel() + "&subtype="+cameraEntity.getSubtype();//大华取码0主码流1子码流2辅码流
                    /*rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/cam/realmonitor?channel=" + cameraEntity.getChannel() + "&subtype=0";//大华取码*/
                    break;
                case Constant.YS://宇视
                    rtsp =  "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/video2";//宇视取码
                    break;
                case Constant.PL://TP-LINK（普联）
                    rtsp = "rtsp://" + cameraEntity.getUsername() + ":" + cameraEntity.getPassword() + "@" + ip + ":"+port+"/stream2";//TP-LINK（普联）取码
                    break;
                default:
                    break;
            }
            cameraEntity.setRtsp(rtsp);
        }
        rtmp = "rtmp://" + IpUtil.IpConvert(config.getStreamNginxIp()) + ":" + config.getStreamNginxTcpPort() + "/live/" + cameraEntity.getToken();
        if (config.getIpExtra().equals("127.0.0.1")) {
            url = rtmp;
        } else {
            url = "rtmp://" + IpUtil.IpConvert(config.getIpExtra()) + ":" + config.getStreamNginxTcpPort() + "/live/" + cameraEntity.getToken();
        }
        String httpUrl = config.getHttp()+"://" + IpUtil.IpConvert(config.getStreamNginxIp()) + ":"+config.getHttpPort() +"/live?port=" + config.getStreamNginxTcpPort() + "&app=live&stream=" + cameraEntity.getToken();//增强Http流
        cameraEntity.setRtmp(rtmp);
        cameraEntity.setUrl(url);
        cameraEntity.setHttpURL(httpUrl);
    }
}
